<?php
/**
 * Created by PhpStorm.
 * User: xiazc01
 * Date: 2018/6/13
 * Time: 14:23
 */
namespace common\repositories\myhtsq;

use common\entities\myhtsq\IncomeItemLogEntity;
use common\repositories\BaseRepository;

class IncomeItemLogRepository extends BaseRepository{


    static $BUSINESS_TYPE_ID_SET = [
        '39bbb94c-b6e3-11e6-91fa-1051721b3b63',
        '45b9476d-d3f9-11e6-91fa-1051721b3b63',
        '7f94a49d-fcf6-11e4-bed8-00155d02c832',
        '7f94a5d3-fcf6-11e4-bed8-00155d02c832',
        '6ee86ae4-7284-11e7-b033-00155d02c83e',
    ];
    static $BUSINESS_TYPE_NAME_SET = [
        '云客互联网服务收入项',
        '云客销售服务收入项',
        '云客租用服务收入项',
        '云客增值服务收入项',
        '云客互联网服务收入项',
    ];

    const OPERATE_DELETE = 'delete';
    const OPERATE_ADD = 'add';
    const OPERATE_EDIT = 'edit';

    public function __construct(array $config = [])
    {
        parent::__construct($config);
    }

    public static function add($before,$now,$success,$operate){
        if(empty($before) && empty($now)){
            return true;
        }
        $map = array_flip(static::$BUSINESS_TYPE_ID_SET);
        $business_type = $now['revenue_type'];
        $key = $map[$business_type];

        if($key >= 0){
            $business_type_name = static::$BUSINESS_TYPE_NAME_SET[$key];
        }else{
            $business_type_name = '未知业务';
        }
        try {
            $date = date('Y-m-d H:i:s');
            $entity = new IncomeItemLogEntity();
            $entity->platform = 'yk';
            $entity->business_type = $business_type_name;
            $entity->contract_guid = $before?$before['contract_id']:$now['contract_id'];
            $entity->operated_on = $date;
            $entity->success = $success;
            $entity->operate = $operate;
            $entity->revenue_id = $before?$before['revenue_id']:$now['revenue_id'];
            $entity->model_diff = urldecode(json_encode(self::diff_model($before,$now,$operate,false)));
            $entity->explain_diff = urldecode(json_encode(self::diff_model($before,$now,$operate,true)));
            $result = $entity->save();
            if ($result === FALSE) {
                $error = $entity->getAllFirstErrorMessage();
                \Yii::info($error,'income_item_log');
            }
        } catch (\Exception $ex) {
            \Yii::info($ex->getMessage(),'income_item_log');
        }
    }

    private static function diff_model($before,$now,$operate,$explain = false){
        $revenue_attribute_label = array(
            'revenue_id' => '收入ID',
            'contract_id' => '合同ID',
            'contract_payover_id' => '交付结算主键ID',
            'revenue_type' => '收入类型id',
            'amount' => '金额',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人',
            'income_code' => '收入项编码',
            'total_confirm_amount' => '',
            'begin_on' => '租期开始日期',
            'end_on' => '租期结束日期',
            'establish_time' => '开通日期',
            'add_type' => '值为2的表示通过开通信息推送产生',
            'recognized_type' => '收入确认方式确定结转规则',
            'unrecognized_bad_debt_amount' => '未确认部分的坏账金额',
            'recognized_bad_debt_amount' => '已经确认部分的坏账金额',
            'kingdee_bad_debt_amount' => '金蝶核销坏账金额',
            'close_status' => '1:收入未关闭;2:收入通过手动关闭;3:收入自动关闭;4:已结转',
            'maintain_remark' => '维护备注',
        );

        $diff = array();
        if($operate == self::OPERATE_DELETE){
            $diff_arr = $before;
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key];
                $record = array('before'=>$diff_arr[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }

        }else if($operate == self::OPERATE_EDIT) {
            $diff_arr = array_diff_assoc($before,$now);
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key] ;
                $record = array('before'=>$before[$key],'now'=>$now[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }
        }else if($operate == self::OPERATE_ADD){
            $diff_arr = $now;
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key] ;
                $record = array('now'=>$diff_arr[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }
        }
        return $diff;
    }
}